package cn.xyinping.pet.service.impl;

import cn.xyinping.basic.exception.MyException;
import cn.xyinping.basic.service.impl.BasicServiceImpl;
import cn.xyinping.basic.util.DistanceUtil;
import cn.xyinping.basic.util.PageList;
import cn.xyinping.basic.util.Point;
import cn.xyinping.org.domain.Employee;
import cn.xyinping.org.domain.Shop;
import cn.xyinping.org.mapper.EmployeeMapper;
import cn.xyinping.org.mapper.ShopMapper;
import cn.xyinping.pet.domain.Pet;
import cn.xyinping.pet.domain.SearchMasterMsg;
import cn.xyinping.pet.mapper.PetMapper;
import cn.xyinping.pet.mapper.SearchMasterMsgMapper;
import cn.xyinping.pet.query.SearchMasterMsgQuery;
import cn.xyinping.pet.service.IPetService;
import cn.xyinping.pet.service.ISearchMasterMsgService;
import cn.xyinping.user.domain.User;
import cn.xyinping.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BasicServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IPetService petService;
    /**
     * 寻主发布业务层
     * @param smm
     */
    @Override
    @Transactional
    public void publish(SearchMasterMsg smm, Long logininfoId) {
        // 1. 校验非空
        if(StringUtils.isEmpty(smm.getName())
                ||StringUtils.isEmpty(smm.getPrice())
                ||StringUtils.isEmpty(smm.getAge())
                ||StringUtils.isEmpty(smm.getGender())
                ||StringUtils.isEmpty(smm.getCoat_color())
//                ||StringUtils.isEmpty(smm.getResources())
                ||StringUtils.isEmpty(smm.getPet_type())
                ||StringUtils.isEmpty(smm.getAddress())
                ||StringUtils.isEmpty(smm.getTitle())){
            throw new MyException("参数不能为空");
        }
        // 2. 根据logininfoId获取登陆用户user_id
        User user = userMapper.findByLofininfoId(logininfoId);
        // 把获取的user_id存在SearchMasterMsg表中
        smm.setUser_id(user.getId());
        smm.setUser(user); // 用户也存进去
        // 3. 获取当前用户输入的位置
        Point point = DistanceUtil.getPoint(smm.getAddress());
        // 获取所有的店铺
        List<Shop> shops = shopMapper.loadAll();
        // 再查询最近的店铺
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        // 根据shopId保存到SearchMasterMsg表中
        smm.setShop(shop);
        smm.setShop_id(shop.getId());
        searchMasterMsgMapper.add(smm);
    }

    /**
     * 未处理
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long logininfoId) {
        // 通过logininfo找到employee
        Employee employee = employeeMapper.findByLogininfo(logininfoId);
        // 判断shop_id是否为空 为空就说明是平台工作人员  不为空就是店长
        if(employee.getShop_id()!=null){ // 不为空就只能在本店铺的员工显示
            query.setShopId(employee.getShop_id());
        }
        // 处理状态设为未处理
        query.setState(0);
        PageList<SearchMasterMsg> pageList = super.queryList(query);
        return pageList;
    }

    /**
     * 已处理
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long logininfoId) {
        // 通过logininfo找到employee
        Employee employee = employeeMapper.findByLogininfo(logininfoId);
        // 判断shop_id是否为空 为空就说明是平台工作人员  不为空就是店长
        if(employee.getShop_id()!=null){ // 不为空就只能在本店铺的员工显示
            query.setShopId(employee.getShop_id());
        }
        // 处理状态设为已处理
        query.setState(1);
        PageList<SearchMasterMsg> pageList = super.queryList(query);
        return pageList;
    }
    /**
     * 已处理修改处理状态
     * @param pet
     * @param logininfoId
     */
    @Override
    @Transactional
    public void handle(Pet pet, Long logininfoId) {
        // 修改处理状态
        searchMasterMsgMapper.updateStateById(pet.getSearch_master_msg_id());
        // 向pet和petDetail中存数据
        petService.add(pet);
    }
}
